<?php

$action = (!empty($_POST['btn_submit']) && ($_POST['btn_submit'] === 'Valider')) ? 'save_article'
                                                                              : 'show_form';
$id = $_REQUEST['id'];

try {

    $mongodb = new MongoClient();
    $articleCollection = $mongodb->blog->articles;

} catch (MongoConnectionException $e) {

    die('Failed to connect to MongoDB '.$e->getMessage());
}

switch($action){
    case 'save_article':        
        $article = array();
        $article['title'] = $_POST['title'];
        $article['content'] = $_POST['content'];
        $article['created_at'] = new MongoDate();
        
        $articleCollection->update(array('_id' => new MongoId($id)), $article);
        break;
    
    case 'show_form':
    default:
        $article = $articleCollection->findOne(array('_id' => new MongoId($id)));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link rel="stylesheet" href="bootstrap.css" />
    	<link rel="stylesheet" href="style.css" />

        <title>MongoBeer</title>
    </head>

    <body>
        <div id="contentarea">
            <div id="innercontentarea">
                <h1>Edition d'un post</h1>
                
                <?php if ($action === 'show_form'): ?>
                
                <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                    <h3>Titre</h3>
                    <p><input type="text" name="title" id="title" value="<?php echo $article['title']; ?>"/></p>
                    <h3>Contenu</h3>
                    <textarea name="content" rows="20"><?php echo $article['content']; ?></textarea>
                    <input type="hidden" name="id" value="<?php echo $article['_id'];?>" />
                    <p><input type="submit" name="btn_submit" value="Valider"/>
                        <input type="button" name="btn_cancer" value="Retour" onClick="history.go(-1)"/></p>
                </form>
                <?php else: ?>
                <p>
                    L'article a bien été modifié.<br/>
                    <br/>
                    <a href="index.php">Retour</a>
                </p>
                <?php endif;?>
                
            </div>
        </div>
    </body>
</html>